//
//  InfoView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/26/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit
class InfoView: UIView {
    
    struct Row {
        var placeholder: String
        var value: String
        var isMarked: Bool
    }
    
    // MARK: - Outlets
    
    lazy private var columnStackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.spacing = 8
        stack.backgroundColor = .green
        stack.alignment = .leading
        stack.distribution = .fillEqually
        return stack
    }()
    public func updateUI (rows: [Row]) {
        for (i, row) in rows.enumerated() {
            let rowSV = (columnStackView.arrangedSubviews[i] as! UIStackView)
            let secondView = rowSV.arrangedSubviews[1]
            if secondView is UILabel {
                (secondView as! UILabel).text = row.value
            } else if secondView is UIView {
                ((secondView as! UIView).viewWithTag(1) as! UILabel).text = row.value
            }
        }
    }
    
    // MARK: - Init
    
    init (rows: [Row]) {
        super.init(frame: .zero)
        self.addSubview(columnStackView)
        self.addUIConstraints()
        for row in rows {
            let rowStack = UIStackView()
            
            let placeholderLbl = UILabel()
            placeholderLbl.text = row.placeholder
            
            let valueLbl = UILabel()
            valueLbl.text = row.value

            self.addViews(rowStack, placeholderLbl, valueLbl)
            rowStack.addArrangedSubview(placeholderLbl)
            if (row.isMarked) {
                let markedView = UIView()
                self.applyMarkedLabel(markedView, valueLbl)
                rowStack.addArrangedSubview(markedView)
            } else {
                rowStack.addArrangedSubview(valueLbl)
            }
            columnStackView.addArrangedSubview(rowStack)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func applyMarkedLabel (_ markedView: UIView, _ label: UILabel) {
        markedView.addSubview(label)
        label.textColor = .white
        label.tag = 1
        markedView.snp.makeConstraints { (make) in
            make.height.equalTo(30)
        }
        label.snp.makeConstraints { (make) in
            make.centerX.centerY.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(8)
        }
        markedView.backgroundColor = .blueColor
        markedView.layer.cornerRadius = 7
    }
    /// Style of One Row
    private func addViews (_ rowStack: UIStackView,  _ placeholderLbl: UILabel, _ valueLbl: UILabel) {
        rowStack.axis = .horizontal
        rowStack.alignment = .center//.leading
        rowStack.distribution = .equalSpacing
        rowStack.translatesAutoresizingMaskIntoConstraints = false
        rowStack.spacing = 12
        rowStack.backgroundColor = .white
        
        placeholderLbl.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        placeholderLbl.sizeToFit()
        placeholderLbl.numberOfLines = 1
        placeholderLbl.textColor = .black
        placeholderLbl.tag = 1
        placeholderLbl.snp.makeConstraints { (make) in
            make.height.equalTo(32)
        }
        
        valueLbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        valueLbl.sizeToFit()
        valueLbl.numberOfLines = 1
        valueLbl.textColor = .black
        valueLbl.tag = 2
    }
    
    /// Constraints of View
    private func addUIConstraints () {
        self.backgroundColor = .rgb(230, 241, 250)
        self.clipsToBounds = false
        self.layer.cornerRadius = 7
        
        columnStackView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(16)
        }
    }
    
}
